package com.ruoyi.framework.web.service;

import com.ruoyi.common.constant.LoginConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.YxDeviceLogUploadRecord;
import com.ruoyi.system.service.IYxDeviceLogUploadRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DeviceLogUploadRecordService {
    @Autowired
    private IYxDeviceLogUploadRecordService yxDeviceLogUploadRecordService;

    public R<String> uploadLogZip(String taskId, String data) {
        //获取当前登录用户
        SysUser user = SecurityUtils.getLoginUser().getUser();

        if(user == null || user.getAccountType() != LoginConstants.USER_TYPE_DEVICE_STU){
            return R.fail("设备信息异常");
        }
        String deviceId = user.getUserName();

        YxDeviceLogUploadRecord query = new YxDeviceLogUploadRecord();
        query.setUuid(taskId);
        query.setDeviceId(deviceId);
        List<YxDeviceLogUploadRecord> list = yxDeviceLogUploadRecordService.selectYxDeviceLogUploadRecordList(query);
        if(!list.isEmpty() && list.size() == 1){
            list.get(0).setLogPath(data);
            list.get(0).setUploadTime(DateUtils.getNowDate());
            yxDeviceLogUploadRecordService.updateYxDeviceLogUploadRecord(list.get(0));
        }

        return R.ok("上传成功");
    }
}
